import { persistReducer, persistStore } from 'redux-persist'

import { combineReducers } from 'redux'
import { configureStore } from '@reduxjs/toolkit'
import storage from 'redux-persist/lib/storage'
import useEchartReducer from './modules/echarts'
import useFrontStore from './modules/front'
import useTabStore from './modules/tabs'
import useUserStore from './modules/user'

const reducers = combineReducers({
  front: useFrontStore,
  tabs: useTabStore,
  echarts: useEchartReducer,
  user: useUserStore
})

const persistConfig = {
  key: 'root',
  storage
}

const persistedReducer = persistReducer(persistConfig, reducers)

const store = configureStore({
  reducer: persistedReducer,
  devTools: process.env.NODE_ENV !== 'production',
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: false
    })
})

export const persistor = persistStore(store)

export type RootState = ReturnType<typeof store.getState>
export type AppDispatch = typeof store.dispatch

export default store
